package com.H1023.service;

import com.H1023.dao.BookMapper;
import com.H1023.pojo.Book;
import com.H1023.pojo.Type;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookServiceImpl implements BookService{
    private BookMapper bookMapper;
    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }



    public List<Type> queryTypeName(){
        return bookMapper.queryTypeName();
    }

    public List<Book> queryByDetailAndAll(String detail) {
        return addTypeName(bookMapper.queryByDetailAndAll(detail),queryTypeName());
    }

    public List<Book> queryAll() {
        return addTypeName(bookMapper.queryAll(),queryTypeName());
    }

    public List<Book> queryByName(String name) {
        return addTypeName(bookMapper.queryByName(name),queryTypeName());
    }

    public List<Book> queryByIsbn(String isbn) {
        return addTypeName(bookMapper.queryByIsbn(isbn),queryTypeName());
    }

    public List<Book> queryByAuthor(String author) {
        return addTypeName(bookMapper.queryByAuthor(author),queryTypeName());
    }

    public List<Book> queryByPress(String press) {
        return addTypeName(bookMapper.queryByPress(press),queryTypeName());
    }

    public List<Book> queryByDetail(String detail) {
        return addTypeName(bookMapper.queryByDetail(detail),queryTypeName());
    }

    public List<Book> queryByType(String type) {
        return addTypeName(bookMapper.queryByType(type),queryTypeName());
    }





    public int queryAllCount() {
        return bookMapper.queryAllCount();
    }

    public int queryCountByTypeName(String type) {
        return bookMapper.queryCountByTypeName(type);
    }

    public List<Book> addTypeName(List<Book> bookList, List<Type> type) {
        for (Book book : bookList) {
                book.setType1(getTypeName(book.getTypeID1(),type));
                book.setType2(getTypeName(book.getTypeID2(),type));
                book.setType3(getTypeName(book.getTypeID3(),type));

        }
        return bookList;
    }



    public String getTypeName(int i,List<Type> types){
        for (Type type : types) {
            if(type.getTypeid()==i)
                return type.getTypename();
        }
        return "0";
    }





}
